From 97fb20dfd682cf2748d1286322455cb8bf8b034d Mon Sep 17 00:00:00 2001
From: Clarke X <1014930533@qq.com>
Date: Sat, 8 Apr 2023 09:56:13 +0800
Subject: [PATCH] overlays: Add gpio-charger support

Add gpio-charger overlay for charger via gpio.

Signed-off-by: Clarke X <1014930533@qq.com>
---
 arch/arm/boot/dts/overlays/Makefile           |  1 +
 arch/arm/boot/dts/overlays/README             | 14 +++++++
 .../dts/overlays/gpio-charger-overlay.dts     | 42 +++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 arch/arm/boot/dts/overlays/gpio-charger-overlay.dts

--- a/arch/arm/boot/dts/overlays/Makefile
+++ b/arch/arm/boot/dts/overlays/Makefile
@@ -68,6 +68,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
 	ghost-amp.dtbo \
 	goodix.dtbo \
 	googlevoicehat-soundcard.dtbo \
+	gpio-charger.dtbo \
 	gpio-fan.dtbo \
 	gpio-hog.dtbo \
 	gpio-ir.dtbo \
--- a/arch/arm/boot/dts/overlays/README
+++ b/arch/arm/boot/dts/overlays/README
@@ -1248,6 +1248,20 @@ Load:   dtoverlay=googlevoicehat-soundca
 Params: <None>
 
 
+Name:   gpio-charger
+Info:   This is a generic overlay for detecting charger with GPIO.
+Load:   dtoverlay=gpio-charger,<param>=<val>
+Params: gpio                    GPIO pin to trigger on (default 4)
+        active_low              When this is 1 (active low), a falling
+                                edge generates a charging event and a
+                                rising edge generates a discharging event.
+                                When this is 0 (active high), this is
+                                reversed. The default is 0 (active high)
+        gpio_pull               Desired pull-up/down state (off, down, up)
+                                Default is "down".
+        type                    Set a charger type for the pin. (Default: mains)
+
+
 Name:   gpio-fan
 Info:   Configure a GPIO pin to control a cooling fan.
 Load:   dtoverlay=gpio-fan,<param>=<val>
--- /dev/null
+++ b/arch/arm/boot/dts/overlays/gpio-charger-overlay.dts
@@ -0,0 +1,42 @@
+// Definitions for gpio-charger module
+/dts-v1/;
+/plugin/;
+
+/ {
+	compatible = "brcm,bcm2835";
+
+	fragment@0 {
+		// Configure the gpio pin controller
+		target = <&gpio>;
+		__overlay__ {
+			pin_state: charger_pins@0 {
+				brcm,pins = <4>; // gpio number
+				brcm,function = <0>; // 0 = input, 1 = output
+				brcm,pull = <1>; // 0 = none, 1 = pull down, 2 = pull up
+			};
+		};
+	};
+	fragment@1 {
+		target-path = "/";
+		__overlay__ {
+			charger: charger@0 {
+				compatible = "gpio-charger";
+				pinctrl-0 = <&pin_state>;
+				status = "okay";
+				gpios = <&gpio 4 0>;
+				charger-type = "mains";
+			};
+		};
+	};
+
+	__overrides__ {
+		gpio =       <&charger>,"reg:0",
+			     <&charger>,"gpios:4",
+			     <&pin_state>,"reg:0",
+			     <&pin_state>,"brcm,pins:0";
+		type =       <&charger>,"charger-type";
+		gpio_pull =  <&pin_state>,"brcm,pull:0";
+		active_low = <&charger>,"gpios:8";
+	};
+
+};
